package View;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Vector;

import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JTextField;
import javax.swing.ListModel;

import javax.swing.WindowConstants;
import javax.swing.SwingUtilities;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

import Controller.Sistema;
import Model.ClienteView;


/**
* This code was edited or generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit www.cloudgarden.com for details.
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
*/
public class BuscarCliente extends javax.swing.JFrame {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private JTextField txtDNI;
	private JTextField txtNombre;
	private JButton btnCerrar;
	private JButton btnAltaLinea;
	private JTextField txtLinea;
	private JButton btnGestionarLinea;
	private JList<ClienteView> lstClientes;
	private JLabel lblLinea;
	private JButton btnModificarCliente;
	private JLabel lblApellido;
	private JLabel lblNombre;
	private JLabel lblDni;
	private JButton btnBuscar;
	private JTextField txtApellido;
	private ClienteView clienteSeleccionado;
	private JFrame buscarCliente;
	Vector<ClienteView> clientesEncontrados;
	ListModel<ClienteView> lstClientesModel;

	/**
	* Auto-generated main method to display this JFrame
	*/
	public static void main(String[] args) {
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				BuscarCliente inst = new BuscarCliente();
				inst.setLocationRelativeTo(null);
				inst.setVisible(true);
			}
		});
	}
	
	public BuscarCliente() {
		super();
		buscarCliente = this;
		initGUI();
	}
	
	private void initGUI() {
		try {
			setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
			getContentPane().setLayout(null);
			{
				txtDNI = new JTextField();
				getContentPane().add(getTxtDNI());
				txtDNI.setBounds(66, 12, 154, 23);
			}
			{
				txtNombre = new JTextField();
				getContentPane().add(txtNombre);
				txtNombre.setBounds(66, 41, 154, 23);
			}
			{
				txtApellido = new JTextField();
				getContentPane().add(txtApellido);
				txtApellido.setBounds(310, 41, 154, 23);
			}
			{
				btnBuscar = new JButton();
				getContentPane().add(btnBuscar);
				btnBuscar.setText("Buscar");
				btnBuscar.setBounds(6, 206, 125, 23);
				btnBuscar.addActionListener(new ActionListener(){
					public void actionPerformed(ActionEvent arg0) {
						btnModificarCliente.setEnabled(false);
						btnAltaLinea.setEnabled(false);
						btnGestionarLinea.setEnabled(false);						
						lstClientes.clearSelection();
						clientesEncontrados = Sistema.getInstancia().buscarCliente(txtDNI.getText(),txtNombre.getText(),txtApellido.getText());
						if(clientesEncontrados != null){
							lstClientesModel = new DefaultComboBoxModel<ClienteView>(clientesEncontrados);
						}else{
							lstClientesModel = new DefaultComboBoxModel<ClienteView>();
						}
						lstClientes.setModel(lstClientesModel);
						lstClientes.setVisible(true);												
					}
				});
			}
			{
				lblDni = new JLabel();
				getContentPane().add(lblDni);
				lblDni.setText("DNI");
				lblDni.setBounds(12, 15, 43, 16);
			}
			{
				lblNombre = new JLabel();
				getContentPane().add(lblNombre);
				lblNombre.setText("Nombre");
				lblNombre.setBounds(12, 44, 44, 16);
			}
			{
				lblApellido = new JLabel();
				getContentPane().add(lblApellido);
				lblApellido.setText("Apellido");
				lblApellido.setBounds(239, 44, 44, 16);
			}
			{
				btnModificarCliente = new JButton();
				getContentPane().add(btnModificarCliente);
				btnModificarCliente.setText("Modificar Cliente");
				btnModificarCliente.setBounds(146, 206, 154, 23);
				btnModificarCliente.setEnabled(false);
				btnModificarCliente.addActionListener(new ActionListener(){
					
					public void actionPerformed(ActionEvent arg0) {						
						ModificarCliente app = new ModificarCliente(clienteSeleccionado);
						app.setVisible(true);
					}
					
				});
			}
			{
				lblLinea = new JLabel();
				getContentPane().add(lblLinea);
				lblLinea.setText("Linea");
				lblLinea.setBounds(239, 15, 47, 16);
			}
			{
				txtLinea = new JTextField();
				getContentPane().add(txtLinea);
				txtLinea.setBounds(310, 12, 154, 23);
			}
			{
				btnAltaLinea = new JButton();
				getContentPane().add(btnAltaLinea);
				btnAltaLinea.setText("Alta Linea Adicional");
				btnAltaLinea.setBounds(311, 206, 154, 23);
				btnAltaLinea.setEnabled(false);
				btnAltaLinea.addActionListener(new ActionListener(){
					public void actionPerformed(ActionEvent arg0) {
						AltaLineaAdicional app = new AltaLineaAdicional(clienteSeleccionado);
						app.setLocationRelativeTo(null);
						app.setVisible(true);
						
					}
					
				});
			}
			{
				btnCerrar = new JButton();
				getContentPane().add(btnCerrar);
				btnCerrar.setText("Cerrar");
				btnCerrar.setBounds(6, 234, 125, 23);
				btnCerrar.addActionListener(new ActionListener(){
					public void actionPerformed(ActionEvent e) {
						buscarCliente.dispose();
					}
					
				});
			}
			{
				lstClientesModel = new DefaultComboBoxModel<ClienteView>();
				lstClientes = new JList<ClienteView>();
				getContentPane().add(lstClientes);
				lstClientes.setModel(lstClientesModel);
				lstClientes.setVisible(false);
				lstClientes.addListSelectionListener(new ListSelectionListener(){
					public void valueChanged(ListSelectionEvent arg0) {
						if(lstClientes.getSelectedIndex() != -1){
							clienteSeleccionado = clientesEncontrados.get(lstClientes.getSelectedIndex());
							btnAltaLinea.setEnabled(true);
							btnModificarCliente.setEnabled(true);
							btnGestionarLinea.setEnabled(true);							
						}
					}
					
				});
				lstClientes.setBounds(12, 92, 452, 102);
			}
			{
				btnGestionarLinea = new JButton();
				getContentPane().add(btnGestionarLinea);
				btnGestionarLinea.setText("Gestionar Lineas");
				btnGestionarLinea.setBounds(146, 234, 154, 23);
				btnGestionarLinea.setEnabled(false);
				btnGestionarLinea.addActionListener(new ActionListener(){

					@Override
					public void actionPerformed(ActionEvent e) {
						GestionLineas app = new GestionLineas(clienteSeleccionado);
						app.setLocationRelativeTo(null);
						app.setVisible(true);
					}
				});
			}

			pack();
			this.setSize(492, 300);
		} catch (Exception e) {
			e.printStackTrace();		}
	}
	
	public JTextField getTxtDNI() {
		return txtDNI;
	}

}
